package cn.t215.cloud.client;

import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Random;

@Slf4j
public class MyRule implements IRule {
    private ILoadBalancer loadBalancer;
    @Override
    public Server choose(Object key) {
        List<Server> servers = loadBalancer.getAllServers();
        Random random = new Random();
        int index = random.nextInt(servers.size());
        log.debug("random:{}", index);
        return servers.get(index);
    }

    @Override
    public void setLoadBalancer(ILoadBalancer lb) {
        this.loadBalancer = lb;
    }

    @Override
    public ILoadBalancer getLoadBalancer() {
        return this.loadBalancer;
    }
}
